package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.mapper.EvaluateMapper;
import com.example.demo.mapper.addressMapper;
import com.example.demo.mapper.cartMapper;
import com.example.demo.mapper.orderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
public class orderController {

    @Autowired
    orderMapper orderMapper;
    @Autowired
    cartMapper cartMapper;
    @Autowired
    EvaluateMapper evaluateMapper;
    @Autowired
    addressMapper addressMapper;

    @GetMapping("/selectorderByuser/{user}")
    public List<order> selectorderByuser(@PathVariable("user") String user) {
        return orderMapper.selsectorderbyuser(user);
    }

    @GetMapping("/selectorderitemByuser/{user}")
    public List<orderitem> selectorderitemByuser(@PathVariable("user") String user) {
        return orderMapper.selectorderitemByuser(user);
    }

    @GetMapping("/selectorderitemByuserandtime/{user}/{time}")
    public List<orderitem> selectorderitemByuserandtime(@PathVariable("user") String user, @PathVariable("time") String time) {

        return orderMapper.selectorderitemByuserandtime(user, time);
    }

    @GetMapping("/addorder/{user}/{actualamountpaid}")
    public order addorder(@PathVariable("user") String user, @PathVariable("actualamountpaid") String actualamountpaid) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = simpleDateFormat.format(new Date());
        order o = new order();
        o.setTime(str);
        o.setUser(user);
        o.setActualamountpaid(actualamountpaid);
        o.setState("待付款");
        List<cart> cartList = cartMapper.selectcartByuser(user);
        if (cartList.size() > 1) {
            List<address> addresses = addressMapper.selectAddress(user);
            for (int i = 0; i < addresses.size(); i++) {
                if (addresses.get(i).getDefaultaddress() == 1) {
                    String dizhi = addresses.get(i).getProvince()+ "," + addresses.get(i).getCity()+ "," + addresses.get(i).getArea()+ "," + addresses.get(i).getDetailed_address() + "," + addresses.get(i).getPhone();
                    o.setAddress(dizhi);
                }
            }
        } else {
            List<address> addresses = addressMapper.selectAddress(user);
            int i = 0;
            String dizhi = addresses.get(i).getProvince()+ "," + addresses.get(i).getCity()+ "," + addresses.get(i).getArea()+ "," + addresses.get(i).getDetailed_address() + "," + addresses.get(i).getPhone();
            o.setAddress(dizhi);
        }
        System.out.println(o);
        orderMapper.addorder(o);
        for (int i = 0; i < cartList.size(); i++) {
            orderitem orderitem = new orderitem();
            orderitem.setColour(cartList.get(i).getColour());
            orderitem.setMemory(cartList.get(i).getMemory());
            orderitem.setName(cartList.get(i).getName());
            orderitem.setPrice(cartList.get(i).getPrice());
            orderitem.setTime(str);
            orderitem.setNumber(cartList.get(i).getNumber());
            orderitem.setUser(cartList.get(i).getUser());
            orderMapper.addoerderitem(orderitem);
            cartMapper.deletebyuser(user);
        }

        return o;
    }

    @GetMapping("/addorder2/{user}/{actualamountpaid}/{colour}/{memory}/{name}/{price}/{number}/{address}")
    public order addorder2(@PathVariable("user") String user, @PathVariable("actualamountpaid") String actualamountpaid, @PathVariable("colour") String colour,
                           @PathVariable("memory") String memory, @PathVariable("name") String name, @PathVariable("price") String price,
                           @PathVariable("number") int number,@PathVariable("address")String address) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = simpleDateFormat.format(new Date());
        order o = new order();
        o.setTime(str);
        o.setUser(user);
        o.setActualamountpaid(actualamountpaid);
        o.setState("待收货");
        o.setAddress(address);
        orderMapper.addorder(o);
        orderitem orderitem = new orderitem();
        orderitem.setColour(colour);
        orderitem.setMemory(memory);
        orderitem.setName(name);
        orderitem.setPrice(price);
        orderitem.setTime(str);
        orderitem.setNumber(number);
        orderitem.setUser(user);
        System.out.println(orderitem);
        orderMapper.addoerderitem(orderitem);
        System.out.println(o);
        return o;
    }

    @GetMapping("/deleteorder/{time}/{user}")
    public order deleteorder(@PathVariable("time") String time, @PathVariable("user") String user) {
        order o = new order();
        o.setUser(user);
        o.setTime(time);
        List<order> os = new ArrayList<>();
        os.add(o);
        orderMapper.deleteorder(os);
        orderitem oi = new orderitem();
        oi.setUser(user);
        oi.setTime(time);
        List<orderitem> ois = new ArrayList<>();
        ois.add(oi);
        orderMapper.deleteorderitem(ois);
        deleteevaluate(user, time);
        return o;
    }


    public evaluate deleteevaluate(String user, String time) {
        evaluate e = new evaluate();
        e.setOrdertime(time);
        e.setUser(user);
        List<evaluate> evaluates = new ArrayList<>();
        evaluates.add(e);
        evaluateMapper.deleteevaluate(evaluates);
        return e;
    }

    @GetMapping("/updateorser/{user}/{time}")
    public order updateorser(@PathVariable("user") String user, @PathVariable("time") String time) {
        order o = new order();
        o.setUser(user);
        o.setTime(time);
        List<order> list = new ArrayList<>();
        list.add(o);
        orderMapper.updateorser(list);
        return o;
    }

    @GetMapping("/updateorser2/{user}/{time}")
    public order updateorser2(@PathVariable("user") String user, @PathVariable("time") String time) {
        order o = new order();
        o.setUser(user);
        o.setTime(time);
        List<order> list = new ArrayList<>();
        list.add(o);
        orderMapper.updateorser2(list);
        return o;
    }
}
